home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / sspell14.zip / STRING.C < prev    next >
C/C++ Source or Header  |  1992-07-06  |  5KB  |  222 lines

  1. /* **************************************************************** */
  2. /*             sspell - similar to Unix spell                       */
  3. /*                        version 1.4                               */
  4. /*                                                                  */
  5. /* Author: Maurice Castro                                           */
  6. /* Release Date:  4 Jul 1992                                         */
  7. /* Bug Reports: maurice@bruce.cs.monash.edu.au                      */
  8. /*                                                                  */
  9. /* This code has been placed by the Author into the Public Domain.  */
  10. /* The code is NOT covered by any warranty, the user of the code is */
  11. /* solely responsible for determining the fitness of the program    */
  12. /* for their purpose. No liability is accepted by the author for    */
  13. /* the direct or indirect losses incurred through the use of this   */
  14. /* program.                                                         */
  15. /*                                                                  */
  16. /* Segments of this code may be used for any purpose that the user  */
  17. /* deems appropriate. It would be polite to acknowledge the source  */
  18. /* of the code. If you modify the code and redistribute it please   */
  19. /* include a message indicating your changes and how users may      */
  20. /* contact you for support.                                         */
  21. /*                                                                  */
  22. /* The author reserves the right to issue the official version of   */
  23. /* this program. If you have useful suggestions or changes for the  */
  24. /* code, please forward them to the author so that they might be    */
  25. /* incorporated into the official version                           */
  26. /*                                                                  */
  27. /* Please forward bug reports to the author via Internet.           */
  28. /*                                                                  */
  29. /* Contributors: mao@physics.su.oz.au                               */
  30. /*                                                                  */
  31. /* **************************************************************** */
  32.  
  33. #include <stdio.h>
  34. #include "string.h"
  35.  
  36. int toupper(a)
  37. int a;
  38. {
  39.     if (('a' <= a) && (a <= 'z'))
  40.        a = a - 'a' + 'A';
  41.     return(a);
  42.     }
  43.  
  44. char *strtok(st,c)
  45. char *st;
  46. char *c;
  47. {
  48.     static char *sthold;
  49.     static forevernull;
  50.     char *stpt;
  51.     int cl;
  52.     int flag;
  53.  
  54.     if (!forevernull) sthold++;
  55.  
  56.     if (st != NULL)
  57.     {
  58.         sthold = st; 
  59.         forevernull = 0;
  60.     }
  61.  
  62.     if (forevernull) return(NULL);
  63.  
  64.     /* skip leading */
  65.     stpt = sthold;
  66.     while (*stpt != NULL)
  67.     {
  68.         cl = 0;
  69.         flag = 1;
  70.         while (c[cl] != NULL)
  71.         {
  72.             if (*stpt == c[cl])
  73.                flag = 0;
  74.             cl++;
  75.             }
  76.         if (flag) break;
  77.         stpt++;
  78.         }
  79.  
  80.     if (*stpt == NULL) return(NULL);  /* hit end of string */
  81.  
  82.     sthold = stpt;
  83.  
  84.     /* if trailing clobber and exit */
  85.     while (*sthold != NULL) 
  86.     {
  87.         cl = 0;
  88.         while (c[cl] != NULL)
  89.     {
  90.         if (*sthold == c[cl])
  91.         {
  92.         *sthold = NULL;
  93.         return(stpt);
  94.         }
  95.         cl++;
  96.         }
  97.     sthold++;
  98.     }
  99.     forevernull = 1;
  100.     return(stpt);
  101.     }
  102.  
  103. char *strstr(cs,ct)
  104. char *cs;
  105. char *ct;
  106. {
  107.     int i;
  108.     int j;
  109.  
  110.     i = 0;
  111.     while (cs[i] != NULL)
  112.     {
  113.     j = 0;
  114.     while ((ct[j] != NULL) && (cs[i+j] == ct[j]))
  115.         j++;
  116.     if (ct[j] == NULL)
  117.         return(&(cs[i]));
  118.     i++;
  119.     }
  120.     return(NULL);
  121.     }
  122.  
  123. void *memcpy(st, ct, n)
  124. char *st;
  125. char *ct;
  126. long n;
  127. {
  128.     long i;
  129.  
  130.     for (i=0; i<n; i++)
  131.     {
  132.         *st = *ct;
  133.         ct++;
  134.         st++;
  135.         }
  136.     return ((void *) st);
  137.     }
  138.  
  139. int memcmp(st, ct, n)
  140. char *st;
  141. char *ct;
  142. long n;
  143. {
  144.     long i;
  145.     
  146.     for (i=0; i <n; i++)
  147.     {
  148.         if (*st < *ct) return (-1);
  149.         if (*st > *ct) return (1); 
  150.         st++;
  151.         ct++;
  152.         }
  153.     return(0);
  154.     }
  155.  
  156. char *strchr(cs, c)
  157. char *cs;
  158. int c;
  159. {
  160.     int csearch;
  161.  
  162.     csearch = (char) c;
  163.     while (*cs != NULL)
  164.     {
  165.         if (*cs == csearch)
  166.            return(cs);
  167.         cs++;
  168.         }
  169.     return(NULL);
  170.     }
  171.  
  172. char *strrchr(cs, c)
  173. char *cs;
  174. int c;
  175. {
  176.     int csearch;
  177.     char *cc;
  178.  
  179.     cc = cs;
  180.     csearch = (char) c;
  181.     while (cs != cc)
  182.     {
  183.         if (*cc == csearch)
  184.            return(cc);
  185.         cc--;
  186.         }
  187.     if (*cc == csearch)
  188.        return(cc);
  189.     return(NULL);
  190.     }
  191.  
  192. int stricmp(a, b)
  193. char *a;
  194. char *b;
  195. {
  196.     while ((*a != NULL) && (*b != NULL) && (toupper(*a) == toupper(*b)))
  197.     {
  198.        a++;
  199.        b++;
  200.        }
  201.     if ((*a == NULL) && (*b == NULL)) return(0); /* they are equal */
  202.     if (toupper(*a) > toupper(*b)) return(1);
  203.     return(-1);
  204.     }
  205.  
  206. int strnicmp(a, b, n)
  207. char *a;
  208. char *b;
  209. {
  210.     int i;
  211.     i = 0;
  212.     while ((*a != NULL) && (*b != NULL) && (toupper(*a) == toupper(*b)) && (++i < n))
  213.     {
  214.        a++;
  215.        b++;
  216.        }
  217.     if (i == n) return(0);
  218.     if ((*a == NULL) && (*b == NULL)) return(0); /* they are equal */
  219.     if (toupper(*a) > toupper(*b)) return(1);
  220.     return(-1);
  221.     }
  222.